package gsde.springboot.eurekaserver;

import org.omg.CORBA.OBJ_ADAPTER;

import java.lang.reflect.GenericArrayType;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Set;
import java.util.Timer;
import java.util.concurrent.ConcurrentHashMap;

public class GsdeServiceStatusManage {

    /**
     * 监控服务收到服务进程信息得次数
     */
    protected final ConcurrentHashMap<String, Object> __receiveTimes = new ConcurrentHashMap();

    protected final ConcurrentHashMap<String, Object> __serviceCheckTimes = new ConcurrentHashMap();


    protected final ConcurrentHashMap<String, GsdeServiceProperty> __lasttimeStatus = new ConcurrentHashMap<>();

    protected final ConcurrentHashMap<String, GsdeServiceProperty> __recentlyStatus = new ConcurrentHashMap<>();
    protected final ConcurrentHashMap<String, Object> __serviceAliveMap = new ConcurrentHashMap<>();
    protected final long __aliveTimeSpace = 120 * 1000; //2 分钟




    protected int  __checkTimes = 0;

    public GsdeServiceStatusManage()
    {

    }

    public void addCheckTimes()
    {
        this.__checkTimes++;

        // 5 次检测一轮
        if (this.__checkTimes >=6)
        {
            this.__checkTimes = 0;
        }

        Set<String> set = this.__receiveTimes.keySet();

        for (String key : set)
        {
            Object serviceCheckTimes = this.__serviceCheckTimes.get(key);

            int tmptimes = 0;
            if (serviceCheckTimes == null)
            {
                tmptimes = 1;
            }
            else
            {
                tmptimes++;
            }

            this.__serviceCheckTimes.put(key, tmptimes);
        }
    }



    public boolean updateAliveStatus(HashMap<String, Object> map)

    {
        Set<String> set = this.__receiveTimes.keySet();
        long receivetimes = 0;

        for (String key : set)
        {
            receivetimes = (Long) this.__receiveTimes.get(key);

            //如果服务进程只发送一次更新消息，状态为活着 ： 1
            if (receivetimes == 1)
            {
                __serviceAliveMap.put(key, 1);
                continue;
            }



        }

        // 获得更新次数




        return false;
    }

    public void putServiceStatus(HashMap<String, Object> map)
    {
        // 服务进程标志
        //String processuuid = (String) map.get("PROCESS_UUID");
        String hostip = (String) map.get("SERVICE_HOST");
        int port = (int) map.get("PORT");
        String serviceName =  (String) map.get("SERVICE_NAME");

        String  processuuid = hostip + ":" + String.valueOf(port) + ":" + serviceName;

        long curtime = System.currentTimeMillis();

        GsdeServiceProperty recentlyProperty= this.__lasttimeStatus.get(processuuid);

        long lastRecentlyUpdateTime = 0;

        boolean recentlyPropertyExists = false;
        //最新收到的
        if (recentlyProperty == null)
        {
            recentlyProperty = new GsdeServiceProperty(hostip, port, serviceName);
            recentlyProperty.setUpdateTime(curtime);
            recentlyPropertyExists = false;
            lastRecentlyUpdateTime = curtime;
        }
        else {
            lastRecentlyUpdateTime = recentlyProperty.getUpdateTime();
            recentlyProperty.setUpdateTime(curtime);
            recentlyPropertyExists = true;
        }

        //上次收到的
        GsdeServiceProperty lasttimeProperty = this.__lasttimeStatus.get(processuuid);

        if (lasttimeProperty == null)
        {
            lasttimeProperty = new GsdeServiceProperty(hostip, port, serviceName);
        }

        lasttimeProperty.setUpdateTime( lastRecentlyUpdateTime );

        this.addReceiveTimes(processuuid);
    }

    public GsdeServiceProperty getLasttimeStatus( String processuuid)
    {
        return this.__lasttimeStatus.get(processuuid);
    }

    public GsdeServiceProperty getRecentlyStatus(String processuuid)
    {
        return this.__recentlyStatus.get(processuuid);
    }

    /**
     * 获得进程收到服务心跳次数
     * @param processuuid
     * @return
     */
    public long getReceiveTimes(String processuuid)
    {
        long times = 0;
        Object obj = this.__receiveTimes.get(processuuid);

        if (obj == null) {
            return times;
        }

        return (long) obj;
    }

    /**
     * 添加收到服务进程心跳信息的次数
     * @param processuuid
     */
    public void addReceiveTimes(String processuuid)
    {
        long times = this.getReceiveTimes(processuuid);

        times++;
        this.__receiveTimes.put(processuuid, times);
    }
}
